package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGeneralCargoManageBinding;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.adapter.transport.CargoManageHomeTabAdapter;
import com.swgk.core.event.EventConstant;
import com.swgk.core.event.MessageEvent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.UserOperating;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: 杨畔
 * @date: 2020/12/28 10:58
 * @Description: 货源管理
 */
public class CargoManageActivity extends AppActivity {

    ActivityGeneralCargoManageBinding binding;
    private boolean mIsGeneralGoods = true;
    //顶部tab
    private CargoManageHomeTabAdapter tabAdapter;

    //跳转操作
    public static void start(Context context, boolean isGeneralGoods) {
        Intent intent = new Intent(context, CargoManageActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isGeneralGoods);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_general_cargo_manage);
        binding.setLifecycleOwner(this);
        initTab();
        initPager();
        initEvent();
        mIsGeneralGoods = getIntent().getBooleanExtra(RouterKey.BOOLEAN, true);
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("普通货源");
        tabs.add("竞价货源");
        tabAdapter = new CargoManageHomeTabAdapter(tabs);
        binding.rvTab.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.rvTab.setAdapter(tabAdapter);
    }

    private void initPager() {
        //设置加载当前页面左右两页内容
        binding.viewpager.setOffscreenPageLimit(1);
        //viewPager适配器
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(CargoManageFragment.newInstance(true));
        fragments.add(CargoManageFragment.newInstance(false));
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), fragments);
        binding.viewpager.setAdapter(pagerAdapter);
    }

    private void initEvent() {
        binding.ivBack.setOnClickListener(v -> finish());
        binding.rightView.setOnClickListener((View) -> {
        /*    if (UserOperating.getInstance().isNotOpenFundAccount(this, PermissionCheckUtil.KEY_HUOYUANGUANLI)) {
                return;
            }*/
            PublishCargoActivity.start(getActivity(), mIsGeneralGoods);
        });
        //
        tabAdapter.setOnItemClickListener((adapter, view, position) -> {
            tabAdapter.setSelectIndex(position);
            binding.viewpager.setCurrentItem(position);
        });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabAdapter.setSelectIndex(position);
                mIsGeneralGoods = (position == 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void fundAccountEvent(MessageEvent event) {
        //开通资金账户后刷新用户数据
        if (event.code == EventConstant.EVENT_REFRESH) {
            UserOperating.getInstance().refresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}